package core.base.exception;

import java.util.Map;
import java.util.TreeMap;

import core.util.MacroVariableInterpreter;
import core.util.MapMacroVariableContainer;

public class AppRTException extends RuntimeException {
	public static final long serialVersionUID = 0;

	private String code;

	private Object[] args;

	private String textMessage;

	private static String formatMessage(String msg, Object[] args) {
		Map<String, Object> map = new TreeMap<String, Object>();
		for (int i = 0; i < args.length; i++) {
			map.put(String.valueOf(i), args[i]);
		}

		MapMacroVariableContainer mvc = new MapMacroVariableContainer(map);
		return MacroVariableInterpreter.interpret(msg, "{", "}", mvc);
	}

	public AppRTException(String code, String msg) {
		super(code + ": " + msg);
		this.code = code;
	}

	public AppRTException(String code, String msg, Throwable cause) {
		super(code + ": " + msg, cause);
		this.code = code;
	}

	public AppRTException(String code, Object[] args, String msg) {
		super(code + ": " + formatMessage(msg, args));
		this.code = code;
		this.args = args;
	}

	public AppRTException(String code, Object[] args, String msg, Throwable cause) {
		super(code + ": " + formatMessage(msg, args), cause);
		this.code = code;
		this.args = args;
	}

	public AppRTException(Throwable cause) {
		super(cause);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String string) {
		code = string;
	}

	public Object[] getArgs() {
		return args;
	}

	public void setArgs(Object[] objects) {
		args = objects;
	}

	public String getTextMessage() {
		return textMessage;
	}

	public void setTextMessage(String textMessage) {
		this.textMessage = textMessage;
	}

}